<template>
  <el-dialog
    :title="editForm.id ? '编辑业务线' : '新建业务线'"
    :visible.sync="isShow"
    width="600px"
    :before-close="close"
    :destroy-on-close="true"
    :lock-scroll="false"
    :close-on-click-modal="false"
    @closed="editForm = {}"
  >
    <el-form
      ref="editForm"
      :model="editForm"
      :rules="editRules"
      label-width="100px"
      @submit.native.prevent
    >
      <el-form-item label="业务线名称:" prop="name">
        <el-input
          v-model="editForm.name"
          placeholder="请输入业务线名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="负责人:" prop="manage">
        <el-select
          v-model="editForm.manage"
          placeholder="请选择负责人"
          style="width: 100%"
          filterable
          remote
          reserve-keyword
          :remote-method="romoteManager"
          :loading="managerLoading"
        >
          <el-option
            v-for="item in userOption"
            :key="'manage' + item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定资产:" prop="asset">
        <el-select
          v-model="editForm.asset"
          placeholder="请选择绑定资产"
          style="width: 100%"
          multiple
          filterable
          remote
          reserve-keyword
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option
            v-for="item in assetOption"
            :key="'asset' + item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务线描述:">
        <el-input
          v-model="editForm.description"
          placeholder="请输入业务线描述"
          :rows="4"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button
        type="primary"
        :loading="editLoading"
        @click="save"
      >保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getAssetList } from '@/api/asset'
import { getUserList } from '@/api/base'
export default {
  name: 'AssetFormDialog',
  props: {
    formData: {
      type: Object,
      required: false,
      default: () => {}
    },
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      editForm: {},
      editRules: { // 编辑弹窗表单验证规则
        name: [
          { required: true, message: '请输入业务线名称', trigger: 'blur' }
        ]
      },
      editLoading: false,
      managerLoading: false,
      loading: false,
      assetType: [], // 资产类型
      userOption: [],
      assetOption: []
    }
  },
  watch: {
    formData(value) {
      this.editForm = Object.assign({}, {
        id: value.id,
        name: value.name,
        manage: value.manage.id,
        description: value.description
      })
    }
  },
  mounted() {
    this._getAssetList()
    this._getUserList()
  },
  methods: {
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.editLoading = true
          let url = ''
          const formData = Object.assign({}, this.editForm)
          if (typeof formData.asset !== 'undefined') {
            formData.asset = formData.asset.toString()
          }
          if (this.editForm.id) {
            url = `/asset/group/update/${this.editForm.id}/`
          } else {
            url = '/asset/group/create/'
          }
          this.$http.post(url, formData).then(res => {
            this.editLoading = false
            if (res.data.code === 0) {
              this.close()
              this.$message({ type: 'success', message: '保存成功' })
              this.$emit('reload-table')
            } else {
              this.$message({ type: 'error', message: res.data.msg })
            }
          }).catch(() => {
            this.editLoading = false
          })
        } else {
          return false
        }
      })
    },
    /* 关闭弹窗 */
    close() {
      this.$emit('close')
    },
    /* 资产绑定远程搜索 */
    async remoteMethod(query) {
      if (query !== '') {
        this.loading = true
        await this._getAssetList(query)
        this.loading = false
      } else {
        await this._getAssetList()
      }
    },
    /* 获得资产列表 */
    _getAssetList(key) {
      getAssetList(key).then(res => {
        this.assetOption = res.data.data
      })
    },
    _getUserList(key) {
      getUserList(key).then(res => {
        this.userOption = res.data.data
      })
    },
    async romoteManager(query) {
      if (query !== '') {
        this.loading = true
        await this._getUserList(query)
        this.loading = false
      } else {
        await this._getUserList()
      }
    }
  }
}
</script>

<style scoped>

</style>
